package com.cskaoyan.config;

import com.cskaoyan.bean.realm.CustomRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("/admin/auth/login","anon");
        map.put("/admin/**","authc");
        map.put("/wx/auth/login","anon");
        map.put("/wx/goods/list","anon");
        map.put("/wx/home/index","anon");
        map.put("/wx/goods/count","anon");
        map.put("/wx/goods/detail","anon");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        shiroFilterFactoryBean.setLoginUrl("localhost:8080");

        return shiroFilterFactoryBean;
    }

    //shiro 核心组件
    @Bean
    public DefaultWebSecurityManager securityManager(CustomRealm customRealm,MarketSessionManger sessionManager){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealms(Arrays.asList(customRealm));
        defaultWebSecurityManager.setSessionManager(sessionManager);
        return defaultWebSecurityManager;
    }

//    @Bean
//    public DefaultWebSessionManager sessionManager(){
//        return new MarketSessionManger();
//    }
}
